package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.BaseContext;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.OrderDto;
import com.itheima.reggie.entity.OrderDetail;
import com.itheima.reggie.entity.Orders;
import com.itheima.reggie.service.OrderDetailService;
import com.itheima.reggie.service.OrdersService;
import com.sun.org.apache.xpath.internal.operations.Or;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/order")
@Slf4j

public class OrderController {

    @Autowired
    private OrdersService ordersService;

    @Autowired
    private OrderDetailService orderDetailService;

    @PostMapping("/submit")
    public R<String> submit(@RequestBody Orders orders) {
        log.info("开始支付");
        ordersService.submit(orders);
        return R.success("支付成功");
    }

    /**
     * 根据订单编号
     * 根据时间区间查询生成的订单明细
     * @param page
     * @param pageSize
     * @param number
     * @param beginTime
     * @param endTime
     * @return
     */
    @GetMapping("/page")
    public R<Page<Orders>> page(int page, int pageSize, String number, String beginTime, String endTime) {
        log.info("订单明细页面分页查询=>{}", page);
        Page<Orders> pageInfo = new Page<>(page, pageSize);

        // 条件构造器对象
        LambdaQueryWrapper<Orders> queryWrapper = new LambdaQueryWrapper<>();
        // 根据订单编号进行查询
        queryWrapper.eq(number != null, Orders::getId, number);
        // 根据订单生成时间进行查询
        queryWrapper.between(beginTime != null && endTime != null, Orders::getCheckoutTime, beginTime, endTime);
        // 定义排序规则
        queryWrapper.orderByDesc(Orders::getCheckoutTime);
        // 分页显示
        ordersService.page(pageInfo, queryWrapper);
        return R.success(pageInfo);
    }

    /**
     * 修改订单状态
     * @param orders
     * @return
     */
    @PutMapping
    public R<String> delivery(@RequestBody Orders orders) {
        Orders ordersServiceById = ordersService.getById(orders);
        ordersServiceById.setStatus(orders.getStatus());
        ordersService.updateById(ordersServiceById);
        return R.success("派送成功");
    }

    /**
     * 在个人中心显示订单信息
     * @return
     */
    @GetMapping("/userPage")
    public R<Page<OrderDto>> userPage(int page, int pageSize) {
        Page<Orders> pageInfo = new Page<>(page, pageSize);

        Page<OrderDto> orderDtoPage = new Page<>(page, pageSize);
        // 获取userId
        Long userId = BaseContext.getCurrentId();

        LambdaQueryWrapper<Orders> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Orders::getUserId, userId);
        // 按照时间升序排列
        queryWrapper.orderByDesc(Orders::getCheckoutTime);

        ordersService.page(pageInfo, queryWrapper);

        // 对dto赋值
        BeanUtils.copyProperties(pageInfo, orderDtoPage, "records");
        // 获取所有订单
        List<Orders> orders = ordersService.list(queryWrapper);

        List<OrderDto> orderDtos = orders.stream().map((item) -> {
            OrderDto orderDto = new OrderDto();
            // 拷贝
            BeanUtils.copyProperties(item, orderDto);
            // 根据订单编号，查询订单明细
            Long orderId = item.getId();

            LambdaQueryWrapper<OrderDetail> lambdaQueryWrapper = new LambdaQueryWrapper<>();
            lambdaQueryWrapper.eq(OrderDetail::getOrderId, orderId);
            // 查询该订单下的所有订单明细
            List<OrderDetail> list = orderDetailService.list(lambdaQueryWrapper);
            orderDto.setOrderDetails(list);
            return orderDto;
        }).collect(Collectors.toList());

        // 对dto的records赋值
        orderDtoPage.setRecords(orderDtos);

        return R.success(orderDtoPage);
    }

    /**
     * 再次生成该订单
     * @param orders
     * @return
     */
    @PostMapping("/again")
    public R<String> again(@RequestBody Orders orders) {
        return R.success("再次生成订单");
    }
}
